/*
 * @Author: Snitro
 * @Date: 2021-05-14 18:02:52
 * @LastEditors: Snitro
 * @LastEditTime: 2021-05-22 22:51:07
 * @Description: file content
 */
#define items_num 3

#include "Data_Painter.h"

uint8_t mod = 0, selector, increase;

int data_array[7] = {0, 0, 1000, 0, 0, 0, 0};
int data_n = 0;

int8_t increase_flag = 0;

int getUicSet() { return data_array[0]; }

int getUidSet() { return data_array[1]; }

int getfidSet() { return data_array[2]; }

void setAvd(int Avd) { data_array[3] = Avd; }

void setRealAvd(int Avd) { data_array[6] = Avd; }

void setRid(int Rid) { data_array[4] = Rid; }

void setDatafH(int fH) { data_array[5] = fH; }

void data_page_press_key_up() {
    if (mod == 0) {
        selector--;
        if (selector >= items_num) selector = items_num - 1;
    } else {
        increase_flag = 0;

        if (selector <= 1) {
            data_n += 10;

            if (selector == 0) {
                if (data_n > 1000) data_n = 0;
            } else {
                if (data_n > 100) data_n = 0;
            }
        } else {
            data_n += 100;

            if (data_n >= 1000000) data_n = 100;
        }
    }

    refresh_settings_page();
}

void data_page_press_key_down() {
    if (mod == 0) {
        selector++;
        if (selector >= items_num) selector = 0;
    }

    refresh_settings_page();
}

void data_page_press_key(int8_t code) {
    if (code == -1) {
        if (!mod) {
            mod = 1;
            data_n = data_array[selector];
            increase_flag = 0;
        } else {
            if ((selector == 0 && data_n > 1000) ||
                (selector == 1 && data_n > 100) ||
                (selector == 2 && data_n > 1000000) ||
                (selector == 2 && data_n < 100)) {
                data_n = data_array[selector];
                increase_flag = 0;
            } else {
                if (data_n != data_array[selector]) {
                    data_array[selector] = data_n;

                    outputer(selector);

                    if (selector < 2) data_Changed();
                }

                mod = 0;
            }
        }
    } else if (code == -2) {
        if (mod) {
            if (data_array[selector] == data_n)
                mod = 0;
            else {
                data_n = data_array[selector];
                increase_flag = 0;
            }
        }
    } else {
        if (increase_flag) {
            if ((selector == 0 && data_n * 10 + code <= 1000) ||
                (selector == 1 && data_n * 10 + code <= 100) ||
                (selector == 2 && data_n * 10 + code <= 1000000))
                data_n = data_n * 10 + code;
        } else
            data_n = code;

        increase_flag = 1;
    }

    refresh_settings_page();
}

void num2str(int num, char* ch) {
    if (num == 0) {
        strcpy(ch, "0");
        return;
    }

    char* temp = ch;

    while (num) {
        *temp = '0' + (num % 10);
        num /= 10;
        temp++;
    }

    for (int i = 0, j = strlen(ch) - 1; i < j; i++, j--) {
        char c = ch[i];
        ch[i] = ch[j];
        ch[j] = c;
    }
}

char* data_cat(int num, char* str) {
    static char ch[10];

    memset(ch, 0, sizeof(ch));

    num2str(num, ch);

    return strcat(ch, str);
}

void refresh_settings_page() {
    static char title[3][10] = {"Uic", "Uid", "fid"};
    static char suffix[3][3] = {"mV", "mV", "HZ"};

    oled_fill(0x00);

    if (mod == 0) {
        oled_p8x16str(20, 0, (int8*)title[0]);
        oled_p8x16str(50, 0, (int8*)data_cat(data_array[0], suffix[0]));
        oled_p8x16str(20, 3, (int8*)title[1]);
        oled_p8x16str(50, 3, (int8*)data_cat(data_array[1], suffix[1]));
        oled_p8x16str(20, 6, (int8*)title[2]);
        oled_p8x16str(50, 6, (int8*)data_cat(data_array[2], suffix[2]));

        oled_p8x16str(0, selector * 3, (int8*)"=>");
    } else {
        oled_p8x16str(0, 0, (int8*)title[selector]);
        oled_p6x8str(
            120 - 6 * strlen(data_cat(data_array[selector], suffix[selector])),
            1, (int8*)data_cat(data_array[selector], suffix[selector]));

        char* ch;
        ch = data_cat(data_n, suffix[selector]);

        oled_p8x16str(64 - 4 * strlen(ch), 4, (int8*)ch);
    }
}

void refresh_data_page() {
    static char title[3][10] = {"Avd", "Rid", "fH"};
    static char suffix[3][4] = {"", "ohm", "HZ"};
    static char ch[10];

    oled_fill(0x00);

    memset(ch, 0, sizeof(ch));

    num2str(data_array[3], ch);

    uint8_t len = strlen(ch);

    ch[len + 1] = 0;
    ch[len] = ch[len - 1];
    ch[len - 1] = ch[len - 2];
    ch[len - 2] = ch[len - 3];
    ch[len - 3] = '.';

    oled_p8x16str(5, 0, (int8*)title[0]);
    oled_p8x16str(35, 0, (int8*)strcat(ch, suffix[0]));

    memset(ch, 0, sizeof(ch));

    num2str(data_array[6], ch);

    len = strlen(ch);

    ch[len + 1] = 0;
    ch[len] = ch[len - 1];
    ch[len - 1] = ch[len - 2];
    ch[len - 2] = ch[len - 3];
    ch[len - 3] = '.';
    ch[6] = 0;

    if (!data_array[6]) strcpy(ch, "NULL");

    oled_p6x8str(92, 1, (int8*)strcat(ch, suffix[0]));
    oled_p8x16str(5, 3, (int8*)title[1]);
    oled_p8x16str(35, 3, (int8*)data_cat(data_array[4], suffix[1]));
    oled_p8x16str(5, 6, (int8*)title[2]);
    oled_p8x16str(35, 6, (int8*)data_cat(data_array[5], suffix[2]));
}